package com.jack.cms.ueditor.hunter;

import java.util.Map;

import com.jack.cms.core.util.SpringContextHolder;
import com.jack.cms.oss.service.FileService;
import com.jack.cms.ueditor.define.AppInfo;
import com.jack.cms.ueditor.define.BaseState;
import com.jack.cms.ueditor.define.MultiState;
import com.jack.cms.ueditor.define.State;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.storage.model.FileListing;

public class FileManager {
	private int count = 0;
	public FileManager ( Map<String, Object> conf ) {
		this.count = (Integer)conf.get( "count" );
	}
	
	public State listFile ( String marker, int index ) {
		FileService fileService=SpringContextHolder.getBean(FileService.class);
		try {
			FileListing files=fileService.list(marker, this.count);
			MultiState state = new MultiState( true );
			BaseState fileState = null;
			for ( FileInfo item : files.items ) {
				if ( item == null ) {
					break;
				}
				fileState = new BaseState( true );
				fileState.putInfo( "url",fileService.getUrl(item.key));
				fileState.putInfo( "time",item.putTime);
				fileState.putInfo( "mimeType",item.mimeType);
				fileState.putInfo( "original",item.key);
				state.addState( fileState );
			}
			state.putInfo( "marker", files.marker );
			state.putInfo( "start", index );
			if(files.isEOF()){
				state.putInfo( "total", index);
			}else{
				state.putInfo( "total", index+this.count+this.count);
			}
			state.putInfo( "total", index+this.count+this.count);
			return state;
		} catch (Exception e) {
			return new BaseState(false, AppInfo.IO_ERROR);
		}
	}
}
